package com.jy.rhin.facade.infra.controller.notification;

import com.jy.framework.base.entity.Page;
import com.jy.framework.base.entity.Result;
import com.jy.rhin.application.infra.NotificationApplicationService;
import com.jy.rhin.infra.repo.notfication.subscribe.query.SubscribeQueryHandler;
import com.jy.rhin.sdk.dto.notification.SubscribeTopicDto;
import com.jy.rhin.sdk.dto.notification.SubscriberAndSubscribeTopicDto;
import com.jy.rhin.sdk.dto.notification.req.CancelSubscribeDto;
import com.jy.rhin.sdk.dto.notification.req.IdDto;
import com.jy.rhin.sdk.dto.notification.req.SubscribePageReqDto;
import com.jy.rhin.sdk.dto.notification.resp.SubscribeDetailDto;
import com.jy.rhin.sdk.dto.notification.resp.SubscribeInfoDto;
import com.jy.rhin.sdk.feign.client.SubscribeFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @author WeiQ
 * @date 2022/6/28 10:50
 * 订阅管理api
 */
@RestController
@RequestMapping("/infra/notification/subscribe")
@Api(tags = "事件总线api", value = "订阅管理api")
@Slf4j
public class SubscribeController implements SubscribeFeign {

    @Resource
    NotificationApplicationService notificationApplicationService;

    @Resource
    SubscribeQueryHandler subscribeQueryHandler;

    @Override
    @ApiOperation("订阅管理api-订阅主题")
    @PostMapping("/subscribeTopic")
    public Result<Long> subscribeTopic(@Valid @RequestBody SubscribeTopicDto dto) {
        return Result.success(notificationApplicationService.subscribe(dto));
    }

    @Override
    @ApiOperation("订阅管理api-取消订阅")
    @PostMapping("/cancelSubscribe")
    public Result<Boolean> cancelSubscribe(@Valid @RequestBody CancelSubscribeDto dto) {
        return Result.success(notificationApplicationService.cancelSubscribe(dto));
    }

    @Override
    @ApiOperation("订阅管理api-分页查询主题订阅列表")
    @GetMapping("/queryPage")
    public Result<Page<SubscribeInfoDto>> queryPage(SubscribePageReqDto dto) {
        return Result.success(subscribeQueryHandler.queryPage(dto));
    }

    @Override
    @ApiOperation("订阅管理api-查询主题订阅详情")
    @GetMapping("/queryDetail")
    public Result<SubscribeDetailDto> queryDetail(IdDto dto) {
        return Result.success(subscribeQueryHandler.queryDetail(dto));
    }

    @Override
    @ApiOperation("订阅管理api-订阅主题(业务服务)")
    @PostMapping("/businessSubscribeTopic")
    public Result<Long> businessSubscribeTopic(@Valid @RequestBody SubscriberAndSubscribeTopicDto dto) {
        return Result.success(notificationApplicationService.businessSubscribeTopic(dto));
    }
    
    @Override
    @ApiOperation("订阅管理api-暂停订阅")
    @PostMapping("/pauseSubscribe")
    public Result<Boolean> pauseSubscribe(@Valid @RequestBody CancelSubscribeDto dto) {
        return Result.success(notificationApplicationService.pauseSubscribe(dto));
    }
    
    @Override
    @ApiOperation("订阅管理api-恢复订阅")
    @PostMapping("/resumeSubscribe")
    public Result<Boolean> resumeSubscribe(@Valid @RequestBody CancelSubscribeDto dto) {
        return Result.success(notificationApplicationService.resumeSubscribe(dto));
    }

}
